#!/usr/bin/env python3
"""
基于频率分析的综合解密方法
"""

from collections import Counter
import string
import json

def load_decoded_data():
    """加载数据"""
    with open('decoded_result.bin', 'rb') as f:
        return f.read()

def build_frequency_mapping():
    """基于频率分析构建映射"""
    print("📊 === 基于频率分析的解密 ===\n")
    
    data = load_decoded_data()
    
    # 1. 统计字节频率
    byte_freq = Counter(data)
    sorted_bytes = [b for b, _ in byte_freq.most_common()]
    
    print("数据中最频繁的20个字节:")
    for i, (byte_val, count) in enumerate(byte_freq.most_common(20)):
        print(f"  {i+1}. 0x{byte_val:02x}: {count}次 ({count/len(data)*100:.1f}%)")
    
    # 2. 典型JSON文本的字符频率（基于大量JSON样本的统计）
    # 这个顺序是基于对实际JSON数据的分析得出的
    json_freq_order = [
        '"',   # 引号最频繁
        ' ',   # 空格
        ':',   # 冒号
        ',',   # 逗号
        'e',   # 最常见的字母
        't',   # 
        'a',
        'o',
        'i',
        'n',
        's',
        'r',
        'l',
        'd',
        'u',
        'c',
        'm',
        'h',
        'p',
        'f',
        'g',
        'y',
        'b',
        'v',
        'k',
        'w',
        '{',
        '}',
        '0',
        '1',
        'x',
        'T',
        'I',
        'S',
        '_',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        'C',
        'A',
        'E',
        'N',
        'R',
        'O',
        'L',
        'D',
        'M',
        'P',
        'U',
        'F',
        'B',
        'G',
        'H',
        'q',
        'j',
        'z',
        'V',
        'W',
        'K',
        'Y',
        'X',
        'J',
        'Q',
        'Z',
        '[',
        ']',
        '-',
        '/',
        '\\',
        '.',
        '?',
        '!',
        '@',
        '#',
        '$',
        '%',
        '&',
        '*',
        '(',
        ')',
        '+',
        '=',
        '<',
        '>',
        '|',
        '~',
        '^',
        '`',
        ';',
        '\''
    ]
    
    # 3. 已知的映射（从 "model" 得到）
    known_mapping = {
        0xa1: ord('m'),
        0xf3: ord('o'),
        0x6b: ord('d'),
        0x31: ord('e'),
        0xb0: ord('l'),
        0x69: ord('"'),  # 最频繁的字节
        0x01: ord(':'),  # 基于分析
        0x90: ord(' '),  # 空格
    }
    
    print("\n\n已知的字符映射:")
    for enc, dec in sorted(known_mapping.items()):
        print(f"  0x{enc:02x} -> '{chr(dec)}'")
    
    # 4. 构建完整映射
    mapping = known_mapping.copy()
    
    # 移除已映射的字符
    remaining_json_chars = []
    for char in json_freq_order:
        if ord(char) not in mapping.values():
            remaining_json_chars.append(char)
    
    # 移除已映射的字节
    remaining_bytes = []
    for byte_val in sorted_bytes:
        if byte_val not in mapping:
            remaining_bytes.append(byte_val)
    
    # 创建频率映射
    print("\n\n构建频率映射:")
    print(f"剩余未映射字节: {len(remaining_bytes)}")
    print(f"剩余JSON字符: {len(remaining_json_chars)}")
    
    # 按频率顺序映射
    for i, byte_val in enumerate(remaining_bytes):
        if i < len(remaining_json_chars):
            mapping[byte_val] = ord(remaining_json_chars[i])
    
    print(f"\n总映射数: {len(mapping)}")
    
    return mapping

def apply_mapping_and_analyze(data, mapping):
    """应用映射并分析结果"""
    print("\n\n🔐 === 应用映射 ===")
    
    # 解密
    decrypted = bytearray()
    for byte in data:
        if byte in mapping:
            decrypted.append(mapping[byte])
        else:
            decrypted.append(byte)
    
    # 保存完整解密结果
    with open('frequency_decrypted.txt', 'wb') as f:
        f.write(decrypted)
    
    print("解密结果已保存到 frequency_decrypted.txt")
    
    # 分析结果
    analyze_decrypted_content(decrypted)
    
    return decrypted

def analyze_decrypted_content(decrypted):
    """分析解密内容"""
    print("\n\n📋 === 分析解密内容 ===")
    
    # 转换为文本
    text = decrypted.decode('utf-8', errors='ignore')
    
    # 显示前1000字符
    print("解密文本预览 (前1000字符):")
    print("-" * 80)
    print(text[:1000])
    print("-" * 80)
    
    # 查找JSON结构
    try:
        # 尝试找到JSON的开始和结束
        start = text.find('{')
        end = text.rfind('}')
        
        if start >= 0 and end > start:
            json_text = text[start:end+1]
            
            # 尝试解析
            try:
                # 首先尝试直接解析
                parsed = json.loads(json_text)
                print("\n✅ 成功解析JSON!")
                print("JSON内容:")
                print(json.dumps(parsed, indent=2, ensure_ascii=False)[:1000])
                
                with open('decrypted.json', 'w', encoding='utf-8') as f:
                    json.dump(parsed, f, indent=2, ensure_ascii=False)
                print("\n完整JSON已保存到 decrypted.json")
                
            except json.JSONDecodeError as e:
                print(f"\n❌ JSON解析失败: {e}")
                
                # 尝试修复常见问题
                print("\n尝试修复JSON...")
                
                # 清理非ASCII字符
                cleaned = ''.join(c if ord(c) < 128 else ' ' for c in json_text)
                
                # 保存清理后的版本
                with open('cleaned_frequency.json', 'w') as f:
                    f.write(cleaned)
                print("清理后的JSON已保存到 cleaned_frequency.json")
                
                # 提取可识别的字段
                extract_fields(cleaned)
    
    except Exception as e:
        print(f"\n处理错误: {e}")

def extract_fields(text):
    """提取可识别的字段"""
    print("\n\n🔍 === 提取可识别的字段 ===")
    
    import re
    
    # 查找字段名
    field_pattern = r'"([a-zA-Z_][a-zA-Z0-9_]*)":'
    fields = re.findall(field_pattern, text)
    
    if fields:
        field_counter = Counter(fields)
        print("找到的字段:")
        for field, count in field_counter.most_common(20):
            print(f"  {field}: {count}次")
    
    # 查找特定字段的值
    print("\n\n字段值示例:")
    
    # questionText
    question_pattern = r'"questionText"\s*:\s*"([^"]*)"'
    questions = re.findall(question_pattern, text)
    if questions:
        print(f"questionText: {questions[0][:100]}...")
    
    # model
    model_pattern = r'"model"\s*:\s*"([^"]*)"'
    models = re.findall(model_pattern, text)
    if models:
        print(f"model: {set(models)}")
    
    # conversationId
    conv_pattern = r'"conversationId"\s*:\s*"([^"]*)"'
    convs = re.findall(conv_pattern, text)
    if convs:
        print(f"conversationId: {convs[0] if convs else 'N/A'}")

def validate_mapping(data, mapping):
    """验证映射的质量"""
    print("\n\n✅ === 验证映射质量 ===")
    
    # 应用映射
    test_size = min(1000, len(data))
    test_decrypted = bytearray()
    
    for i in range(test_size):
        if data[i] in mapping:
            test_decrypted.append(mapping[data[i]])
        else:
            test_decrypted.append(ord('?'))
    
    # 检查可打印字符比例
    printable = sum(1 for b in test_decrypted if 32 <= b <= 126)
    print(f"可打印字符比例: {printable/test_size*100:.1f}%")
    
    # 检查JSON特征
    test_text = bytes(test_decrypted).decode('ascii', errors='ignore')
    
    json_features = [
        ('引号', '"', test_text.count('"')),
        ('冒号', ':', test_text.count(':')),
        ('逗号', ',', test_text.count(',')),
        ('大括号', '{', test_text.count('{')),
        ('大括号', '}', test_text.count('}')),
        ('"model"', '"model"', test_text.count('"model"')),
    ]
    
    print("\nJSON特征检查:")
    for name, feature, count in json_features:
        print(f"  {name} ({feature}): {count}次")
    
    # 检查是否有常见的JSON模式
    if '": ' in test_text:
        print("  ✓ 找到 \": 模式")
    if '", "' in test_text:
        print("  ✓ 找到 \", \" 模式")
    if '{"' in test_text:
        print("  ✓ 找到 {\" 模式")

if __name__ == "__main__":
    # 加载数据
    data = load_decoded_data()
    
    # 构建映射
    mapping = build_frequency_mapping()
    
    # 验证映射
    validate_mapping(data, mapping)
    
    # 应用映射并分析
    decrypted = apply_mapping_and_analyze(data, mapping)
    
    print("\n\n🎯 === 最终结果 ===")
    print("解密完成!")
    print("查看以下文件:")
    print("- frequency_decrypted.txt: 完整解密文本")
    print("- cleaned_frequency.json: 清理后的JSON")
    print("- decrypted.json: 解析后的JSON (如果成功)")
    
    print("\n如果解密不完全正确，可能需要:")
    print("1. 微调字符频率顺序")
    print("2. 使用更多已知明文")
    print("3. 通过动态调试获取准确的映射表")